package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.widget.QuitDialog;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class RegisterActivity extends BaseActivity {
	/** Called when the activity is first created. */
	private EditText reg_editor_username, reg_editor_password,
			reg_editor_passwordrepeat;
	private Button reg_button_commit;
	private TradeApp app;
	private TextView jump;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!(ActivityTaskManager.getInstance()
				.containsName("RegisterActivity"))) {
			ActivityTaskManager.getInstance().putActivity("RegisterActivity",
					this);
		}
		app = getMyApplication();
		setContentView(R.layout.register);
		setHeader(5);
		reg_editor_username = (EditText) findViewById(R.id.reg_editor_username);
		reg_editor_password = (EditText) findViewById(R.id.reg_editor_password);
		reg_editor_passwordrepeat = (EditText) findViewById(R.id.reg_editor_passwordrepeat);
		reg_button_commit = (Button) findViewById(R.id.reg_button_commit);
		jump=(TextView)findViewById(R.id.reg_text_jump);
		reg_button_commit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 注册
				progressDialog = ProgressDialog.show(RegisterActivity.this, null, "正在提交数据数据，请稍后.....", true, true);
				regTask();
			}
		});
		jump.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 跳到登陆
				Intent intent = new Intent(RegisterActivity.this,
						LoginActivity.class);
				intent.putExtra("status","1");
				startActivity(intent);
				//RegisterActivity.this.finish();
			}
		});
	}

	private void regTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "registerUser");
		map.put("username", reg_editor_username.getText().toString().trim());
		map.put("password", reg_editor_password.getText().toString().trim());
		map.put("password", reg_editor_passwordrepeat.getText().toString()
				.trim());
		map.put("imei", app.getImei());
		map.put("imsi", app.getImsi());
		map.put("version", app.getVersion());
		map.put("phone", app.getPhone());
		map.put("phonetype", app.getPhoneType());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					if (response.getString("succeed").equals("000")) {
						Toast toast = Toast.makeText(RegisterActivity.this,
								"注册成功", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
						Intent intent = new Intent(RegisterActivity.this,
								UserCenterActivity.class);
						intent.putExtra("status","1");
						startActivity(intent);
						
					} else if (response.getString("succeed").equals("001")) {
						Toast toast = Toast.makeText(RegisterActivity.this,
								"后台出错,请与后台管理员联系", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else if (response.getString("succeed").equals("002")) {
						Toast toast = Toast.makeText(RegisterActivity.this,
								"用户名密码格式不正确,请检查", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} 
				} catch (JSONException e) {
					Toast toast = Toast.makeText(RegisterActivity.this,
							"网络超时。", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					e.printStackTrace();
				}finally {
					if (progressDialog != null)
						progressDialog.dismiss();
					progressDialog = null;
				}

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(RegisterActivity.this, "网络超时。",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (Constant.debug)
				Log.d("TMG", "ManageActivity KEYCODE_BACK");
			QuitDialog dialog = new QuitDialog(RegisterActivity.this);
			dialog.exitConfirm();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}